/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlRoleManagement.java
 *
 * Created on 12-08-2011, 09:27:27
 */

package tuitionmanagement.gui;

import tuitionmanagement.DAO.RoleFee;
import tuitionmanagement.entity.Role;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author NHAN
 */
public class pnlRoleManagement extends javax.swing.JPanel {
    Role role = new Role();
    RoleFee buz = new RoleFee();
    int index;
    int selectRow = -1;
    public int confirm;
    ArrayList<Role> lst;
    
    /** Creates new form pnlRoleManagement */

    public pnlRoleManagement() {
             initComponents();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        lst = buz.readByAll();
        loadTableAccountDetail(index);
    }
 public void loadTableAccountDetail(ArrayList<Role> lst) {
        Vector vRow = new Vector();
        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            role = lst.get(i);
            v.addElement(role.getRoleId());
            v.addElement(role.getRoleName());           
            vRow.addElement(v);
        }
        Vector vCol = new Vector();
        vCol.addElement("RoleID");
        vCol.addElement("Role Name");
        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tblRole.setModel(tblModel);
    }

    public void loadAllactiAccountDetail() {
        ArrayList<Role> lst = buz.readByAll();
        loadTableAccountDetail(lst);
    }

    private void loadTableAccountDetail(int index) {

        ArrayList<Role> lst = buz.readByAll();
        for (int i = 0; i < lst.size(); i++) {
            if (index != lst.get(i).getRoleId()) {
                lst.remove(i);
                i--;
            }
        }
        loadAllactiAccountDetail();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        pnlList = new javax.swing.JPanel();
        lblListComment = new javax.swing.JLabel();
        scpGrade = new javax.swing.JScrollPane();
        tblRole = new javax.swing.JTable();
        pnlFunction = new javax.swing.JPanel();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        pnlManage = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        pnlDetail = new javax.swing.JPanel();
        lblRoleName = new javax.swing.JLabel();
        txtRoleName = new javax.swing.JTextField();
        lblRoleID = new javax.swing.JLabel();
        txtRoleID = new javax.swing.JTextField();
        lblComment = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        pnlTitle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlTitle.setLayout(new java.awt.GridBagLayout());

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblTitle.setForeground(new java.awt.Color(0, 51, 153));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        lblTitle.setText("ROLE MANAGEMENT");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        pnlTitle.add(lblTitle, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 10, 10);
        add(pnlTitle, gridBagConstraints);

        pnlList.setForeground(new java.awt.Color(255, 255, 255));
        pnlList.setLayout(new java.awt.GridBagLayout());

        lblListComment.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblListComment.setForeground(new java.awt.Color(0, 51, 153));
        lblListComment.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblListComment.setText("Role List");
        lblListComment.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        pnlList.add(lblListComment, gridBagConstraints);

        tblRole.setFont(new java.awt.Font("Tahoma", 0, 12));
        tblRole.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "RoleID", "Role Name"
            }
        ));
        tblRole.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblRoleMouseClicked(evt);
            }
        });
        scpGrade.setViewportView(tblRole);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlList.add(scpGrade, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 166;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 10);
        add(pnlList, gridBagConstraints);

        pnlFunction.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Function", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlFunction.setLayout(new java.awt.GridBagLayout());

        btnCancel.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.setEnabled(false);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 15, 5);
        pnlFunction.add(btnCancel, gridBagConstraints);

        btnSave.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/save_as.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 15, 50);
        pnlFunction.add(btnSave, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(pnlFunction, gridBagConstraints);

        pnlManage.setLayout(new java.awt.GridBagLayout());

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add New Role");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnAdd, gridBagConstraints);

        btnDelete.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete Role");
        btnDelete.setEnabled(false);
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnDelete, gridBagConstraints);

        btnEdit.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit Role");
        btnEdit.setEnabled(false);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnEdit, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(pnlManage, gridBagConstraints);

        pnlDetail.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Role Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlDetail.setLayout(new java.awt.GridBagLayout());

        lblRoleName.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblRoleName.setText("Role Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 70, 5, 5);
        pnlDetail.add(lblRoleName, gridBagConstraints);

        txtRoleName.setEditable(false);
        txtRoleName.setFont(new java.awt.Font("Tahoma", 0, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 70);
        pnlDetail.add(txtRoleName, gridBagConstraints);

        lblRoleID.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblRoleID.setText("RoleID");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 70, 5, 5);
        pnlDetail.add(lblRoleID, gridBagConstraints);

        txtRoleID.setBackground(new java.awt.Color(233, 229, 214));
        txtRoleID.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 70);
        pnlDetail.add(txtRoleID, gridBagConstraints);

        lblComment.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblComment.setText("Information of role that will to be saved into database");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.7;
        gridBagConstraints.insets = new java.awt.Insets(20, 20, 5, 20);
        pnlDetail.add(lblComment, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.7;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(pnlDetail, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tblRoleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblRoleMouseClicked
        // TODO add your handling code here:
        
         int row = tblRole.getSelectedRow();
        if (row < 0) {
            return;
        }
        txtRoleID.setText(tblRole.getValueAt(row, 0).toString());
        txtRoleName.setText(tblRole.getValueAt(row, 1).toString());
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        btnSave.setEnabled(false);
        loadAllactiAccountDetail();
        tblRole.updateUI();
       
    }//GEN-LAST:event_tblRoleMouseClicked

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        txtRoleID.setText("");
        txtRoleName.setText("");
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:

        if (!btnEdit.isEnabled()) {
            if (txtRoleName.getText().trim().equals("")
                    || txtRoleID.getText().trim().equals("") ) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtRoleName.requestFocus();
                return;
            }           
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New Role", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                role.setRoleId(Integer.parseInt(txtRoleID.getText()));
                role.setRoleName(txtRoleName.getText());            
                if (buz.create(role)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtRoleID.setText(null);
                    txtRoleName.setText(null);
                    txtRoleName.requestFocus();
                    btnEdit.setEnabled(false);
                    btnDelete.setEnabled(false);
                    loadAllactiAccountDetail();
                    this.tblRole.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        } else {
            txtRoleID.setText(null);
            txtRoleName.setText(null);
            txtRoleName.requestFocus();
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
        }



    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
          if (btnSave.isEnabled()) {
            JOptionPane.showMessageDialog(this, "This function is being chosen!");
        } else {
            btnDelete.setEnabled(false);
            btnEdit.setEnabled(false);
            btnSave.setEnabled(true);
            btnCancel.setEnabled(true);
            txtRoleID.setEditable(true);
            txtRoleName.setEditable(true);
            txtRoleID.setText("");
            txtRoleName.setText("");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
         if (txtRoleID.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose a role from the table!");
        } else {
            int roleId = Integer.parseInt(txtRoleID.getText());
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Role", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                if (buz.remove(roleId)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtRoleID.setText(null);
                    txtRoleName.setText(null);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactiAccountDetail();
                    this.tblRole.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }

        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
         if (txtRoleID.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose a role from the table!");
        } else {
            txtRoleID.setEditable(true);
            txtRoleName.setEditable(true);

            if (txtRoleName.getText().trim().equals("")
                    || txtRoleID.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtRoleName.requestFocus();
                return;
            }
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update Role", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                role.setRoleId(Integer.parseInt(txtRoleID.getText()));
                role.setRoleName(txtRoleName.getText());
                if (buz.store(role)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtRoleID.setText(null);
                    txtRoleName.setText(null);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactiAccountDetail();
                    this.tblRole.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        }
    }//GEN-LAST:event_btnEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblComment;
    private javax.swing.JLabel lblListComment;
    private javax.swing.JLabel lblRoleID;
    private javax.swing.JLabel lblRoleName;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel pnlDetail;
    private javax.swing.JPanel pnlFunction;
    private javax.swing.JPanel pnlList;
    private javax.swing.JPanel pnlManage;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JScrollPane scpGrade;
    private javax.swing.JTable tblRole;
    private javax.swing.JTextField txtRoleID;
    private javax.swing.JTextField txtRoleName;
    // End of variables declaration//GEN-END:variables

}
